package com.xueyi.tesla.mapper;

import com.xueyi.game.backend.entity.dto.ChangeRoleStatusDto;
import com.xueyi.game.backend.entity.po.BaseRole;
import com.xueyi.game.backend.entity.po.BaseRoleJoinAuthority;
import com.xueyi.game.backend.entity.vo.BaseSelectVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @authoer: hxs
 * @createDate: 2022/11/9 09:40
 * @description: 角色管理mapper
 */
public interface BaseRoleMapper {
    /**
     * 查询角色列表
     *
     * @param roleName      角色名
     * @param isValid       角色状态
     * @param startIndex    起始位置
     * @param limit         查询条数
     * @param roleTableName 表名称
     * @return 角色列表
     * @author hxs
     */
    @Select({
            " <script>" +
                    " select * from ${roleTableName} " +
                    " <where> " +
                    "   <if test=' roleName!=null '>" +
                    "       and role_name like '%${roleName}%'   " +
                    "   </if> " +
                    "   <if test=' isValid!=null '>" +
                    "       and is_valid = #{isValid}   " +
                    "   </if> " +
                    " </where>" +
                    "  limit #{startIndex} , #{limit}  " +
                    "</script> "
    })
    List<BaseRole> findRoleList(@Param("roleName") String roleName, @Param("isValid") Integer isValid, @Param("startIndex") Integer startIndex,
                                @Param("limit") Integer limit, @Param("roleTableName") String roleTableName);


    /**
     * 查询角色列表总条数
     *
     * @param roleName      角色名
     * @param isValid       是否有效
     * @param roleTableName 表名
     * @return 角色列表总条数
     * @author hxs
     */
    @Select({
            " <script>" +
                    " select count(id) from ${roleTableName} " +
                    " <where> " +
                    "   <if test=' roleName!=null '>" +
                    "       and role_name like '%${roleName}%'   " +
                    "   </if> " +
                    "   <if test=' isValid!=null '>" +
                    "       and is_valid = #{isValid}   " +
                    "   </if> " +
                    " </where>" +
                    "</script> "
    })
    Integer findRoleListTotal(@Param("roleName") String roleName, @Param("isValid") Integer isValid, @Param("roleTableName") String roleTableName);


    /**
     * 新增角色
     *
     * @param role          角色对象
     * @param roleTableName 表名称
     * @author hxs
     */
    @Insert({
            " insert into ${roleTableName} ( role_name , is_valid ) values ( #{role.roleName} , #{role.isValid} ) "
    })
    void addRole(@Param("role") BaseRole role, @Param("roleTableName") String roleTableName);

    /**
     * 修改角色
     *
     * @param role          角色对象
     * @param roleTableName 表名称
     * @author hxs
     */
    @Update({
            " update ${roleTableName} set role_name = #{role.roleName} where id = #{role.id} "
    })
    void updateRole(@Param("role") BaseRole role, @Param("roleTableName") String roleTableName);

    /**
     * 修改角色的状态
     *
     * @param changeRoleStatusReq
     * @return
     * @author hxs
     */
    @Update({
            " update  ${roleTableName} set is_valid = #{changeRoleStatusReq.isValid} where id = #{changeRoleStatusReq.id} "
    })
    void changeRoleStatus(@Param("changeRoleStatusReq") ChangeRoleStatusDto changeRoleStatusReq, @Param("roleTableName") String roleTableName);

    /**
     * 解除角色与权限之间的关系
     *
     * @param roleId             角色id
     * @param authorityTableName 表名称
     * @author hxs
     */
    @Delete({
            " delete from ${authorityTableName} where role_id = #{roleId} "
    })
    void relieveJurisdictionByRoleId(@Param("roleId") Integer roleId, @Param("authorityTableName") String authorityTableName);

    /**
     * 为角色分配权限
     *
     * @param list               角色权限对象集合
     * @param authorityTableName 表名称
     * @author hxs
     */
    @Insert({
            " <script> " +
                    " insert into ${authorityTableName} ( role_id , menu_id ) values " +
                    "  <foreach collection='list' item = 'item' index='index' separator = ',' >" +
                    "  ( #{item.roleId},#{item.menuId} ) " +
                    "  </foreach> " +
                    " </script> "
    })
    void bindJurisdictionByRoleId(@Param("list") List<BaseRoleJoinAuthority> list, @Param("authorityTableName") String authorityTableName);

    /**
     * 通过角色id查询该角色的权限id列表
     *
     * @param roleId             角色id
     * @param authorityTableName 表名称
     * @return
     * @author hxs
     */
    @Select({
            " select GROUP_CONCAT(menu_id) from ${authorityTableName} where role_id = #{roleId} "
    })
    String findJurisdictionListByRoleId(@Param("roleId") Integer roleId, @Param("authorityTableName") String authorityTableName);

    /**
     * 获取所有的角色（下拉菜单用）
     *
     * @param roleTableName 表名称
     * @return
     * @author hxs
     */
    @Select({
            " <script> select id value, role_name label from ${roleTableName} where is_valid = 1 " +
                    " <if test = ' id != null '> " +
                    "  and is_show = 1 " +
                    " </if>  " +
                    " </script> "
    })
    List<BaseSelectVo> findAllRoleSelectVo(@Param("roleTableName") String roleTableName, @Param("id") String id);

    /**
     * 获取绑定了该角色的用户
     *
     * @param roleIds               角色id
     * @param userJoinRoleTableName 表名
     */
    // @Select({
    //         " select user_id from ${table} where is_valid=1 and role_id  = #{roleId} "
    // })
    // List<Integer> findUserByRoleId(@Param("roleId") Integer roleId, @Param("table") String userJoinRoleTableName);
    @Select({
            " select user_id from ${table} where is_valid=1 and role_id in (${roleIds}) "
    })
    List<Integer> findUserByRoleId(@Param("roleIds") String roleIds, @Param("table") String userJoinRoleTableName);
}
